<?php                                     
error_reporting(E_ALL);
                                                  
ini_set('display_errors', true);
date_default_timezone_set('Europe/London');
                                                  
define('DEFAULT_CONTROLLER','file_manager');
define('DEFAULT_ACTION','index');

define('ROOT_DIR', rtrim(dirname(__FILE__),'/'));

define('SITE_FOLDER', 'RedPanda_FileManager/');

define('VIEWS_FOLDER', ROOT_DIR.'/app/views/');

if(isset($_SERVER['HTTP_HOST'])){
    define('SITE_URL','http://'.rtrim($_SERVER['HTTP_HOST'],'/').'/'.SITE_FOLDER);
}
else{
    define('SITE_URL','http://'.rtrim($_SERVER['SERVER_NAME'],'/').'/'.SITE_FOLDER);
}

set_include_path(ROOT_DIR.'/app/views'.PATH_SEPARATOR.PATH_SEPARATOR.ROOT_DIR.'/app/models'.PATH_SEPARATOR. get_include_path());



// get rid of the magic quotes nuisance
function stripslashes_deep($value){
    return (is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value));
}

if (get_magic_quotes_gpc()){
  $_GET = array_map('stripslashes_deep', $_GET);
  $_POST = array_map('stripslashes_deep', $_POST);
  $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
  $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

ini_set('magic_quotes_runtime', false);                                     


///////////////////////////////////////////

require_once('app/classes/controller.php');
require_once('app/classes/front_controller.php');
require_once('app/classes/model.php');
require_once('app/classes/view.php');
require_once('app/classes/exceptions.php');

function __autoload($class_name) {
	
    $filename = strtolower(ltrim($class_name,'Ab_')).'.php';
   
    if(file_exists(ROOT_DIR . '/app/models/' . $filename)){
        include (ROOT_DIR . '/app/models/' . $filename);
    }
  
    if(file_exists(ROOT_DIR . '/app/controllers/' . $filename)){    	
        include (ROOT_DIR . '/app/controllers/' . $filename);
    }
    
}

require_once('config.php');
session_start();
             
try{                                                               
	// set up controller
	$frontController = new Ab_FrontController();

	$frontController->dispatch();
	echo $frontController->render();
}
catch(NotFoundException $e){
    header('HTTP/1.1 404 File Not Found Error');
    die('The file was not found');
}
catch(ViewNotFoundException $e){
    header('HTTP/1.1 404 File Not Found Error');
     var_dump($e);
    die('The file was not found');
}
catch(Exception $e){
	
    header('HTTP/1.1 500 Internal Server Error');
    var_dump($e);
    die('Unexpected error. Please try again later.');
}

?>
